/*
 * Created: Author: Aléx Carvalho / Date: Feb 13, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-04-10 04:14:26 +0000 (Fri, 10 Apr 2015) $
 */
package com.cpm.view.zk.tree.user;

import com.cpm.ServiceLocator;
import com.cpm.view.IconKit;
import com.cpm.entities.User;
import com.cpm.view.zk.tree.AbstractTree;
import com.cpm.view.zk.tree.DefaultTreeNode;
import java.util.List;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

public class UserTree extends AbstractTree<User> {

    public UserTree() {
    }

    @Override
    protected void buildRoot(DefaultTreeNode root) {
        List<User> users = (List<User>) ServiceLocator.getUserRepository().findAll();

        users.forEach( u -> {
            DefaultTreeNode<User> node = new DefaultTreeNode<>(u);
            root.add(node);
        });
    }

    @Override
    protected void render(Treeitem item, DefaultTreeNode<User> node, int i) {
        item.setValue(node);
        
        Treerow row = item.getTreerow();
        
        if (row == null){
            row = new Treerow();
            row.setParent(item);
        }
        
        User user = node.getData();
        
        Treecell cell = new Treecell();
        cell.setParent(row);
        cell.setLabel(user.getName());
        cell.setIconSclass(IconKit.USER.getValue());
        
    }
}
